/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.campaign_service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class CampaignResponseParser {

	Document _XMLDocument;


	/*
	 * Constructor
	 */
	public CampaignResponseParser(String XMLData){

		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}//Constructors
	
	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	        
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Parse campaign list data
	 */
	public ArrayList<HashMap<String, String>> getCampaignList(){

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
			
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList name = element.getElementsByTagName("name");
	        	temp.put("name", XMLUtils.getCharacterDataFromElement((Element) name.item(0)));

	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));
	        	
	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));
	        	//temp.put("clicks", "2");

	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
	        	NodeList costAmount = costElement.getElementsByTagName("microAmount");
	        	temp.put("cost", 
	        			Converter.microamountToStr(XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
	        					));
	        	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));
	        	
	        	NodeList conv = element.getElementsByTagName("conversions");
	        	temp.put("conv", XMLUtils.getCharacterDataFromElement((Element) conv.item(0)));

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList biddingStrategy = element.getElementsByTagName("BiddingStrategy.Type");
	        	temp.put("biddingStrategy", XMLUtils.getCharacterDataFromElement((Element) biddingStrategy.item(0)));
	        	
	        	NodeList budget = element.getElementsByTagName("budget");
	        	Element budgetElement = (Element) budget.item(0);
	        	NodeList budgetAmount = budgetElement.getElementsByTagName("microAmount");
	        	temp.put("budget", 
	        			Converter.microamountToStr(XMLUtils.getCharacterDataFromElement((Element) budgetAmount.item(0))
	        					));

	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	//temp.put("impressions", "10");
	        	
	        	Act_Super.uppendStatsToTotal(temp);
	        	
	        	list.add(temp);
	        }
	        
	        Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}//getCampaignList
	
	
	
	/*
	 * Parse campaign data, to display in edit form
	 * Only one campaign
	 */
	public Map<String, String> getCampaignData(){
		Map<String, String> campaignData = new HashMap<String, String>();
		
		try {
	        
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");//<--- put valid tag

        	Element element = (Element) nodes.item(0);

        	NodeList name = element.getElementsByTagName("name");
        	campaignData.put("name", XMLUtils.getCharacterDataFromElement((Element) name.item(0)));
        	
        	NodeList budget = element.getElementsByTagName("budget");
        	Element budgetElement = (Element) budget.item(0);
        	NodeList budgetAmount = budgetElement.getElementsByTagName("microAmount");
        	campaignData.put("budget", XMLUtils.getCharacterDataFromElement((Element) budgetAmount.item(0)));

        	NodeList targetGoogleSearch = element.getElementsByTagName("targetGoogleSearch");
        	campaignData.put("targetGoogleSearch", XMLUtils.getCharacterDataFromElement((Element) targetGoogleSearch.item(0)));
        	
        	NodeList targetSearchNetwork = element.getElementsByTagName("targetSearchNetwork");
        	campaignData.put("targetSearchNetwork", XMLUtils.getCharacterDataFromElement((Element) targetSearchNetwork.item(0)));
        	
        	NodeList targetContentNetwork = element.getElementsByTagName("targetContentNetwork");
        	campaignData.put("targetContentNetwork", XMLUtils.getCharacterDataFromElement((Element) targetContentNetwork.item(0)));
        	
        	NodeList targetContentContextual = element.getElementsByTagName("targetContentContextual");
        	campaignData.put("targetContentContextual", XMLUtils.getCharacterDataFromElement((Element) targetContentContextual.item(0)));
        	
        	NodeList targetPartnerSearchNetwork = element.getElementsByTagName("targetPartnerSearchNetwork");
        	campaignData.put("targetPartnerSearchNetwork", XMLUtils.getCharacterDataFromElement((Element) targetPartnerSearchNetwork.item(0)));
        	
        	NodeList BiddingStrategyType = element.getElementsByTagName("BiddingStrategy.Type");
        	campaignData.put("BiddingStrategyType", XMLUtils.getCharacterDataFromElement((Element) BiddingStrategyType.item(0)));

        	NodeList deliveryMethod = element.getElementsByTagName("deliveryMethod");
        	campaignData.put("deliveryMethod", XMLUtils.getCharacterDataFromElement((Element) deliveryMethod.item(0)));
        	
        	NodeList adServingOptimizationStatus = element.getElementsByTagName("adServingOptimizationStatus");
        	campaignData.put("adServingOptimizationStatus", XMLUtils.getCharacterDataFromElement((Element) adServingOptimizationStatus.item(0)));

        	NodeList bidLimit = element.getElementsByTagName("biddingStrategy");
        	Element bidLimitElement = (Element) bidLimit.item(0);
        	NodeList bidLimitAmount = bidLimitElement.getElementsByTagName("microAmount");
        	campaignData.put("bidLimit", XMLUtils.getCharacterDataFromElement((Element) bidLimitAmount.item(0)));

	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }

		return campaignData;
	}//getCampaignData	
	
	
	
	/*
	 * Get Campaign Id
	 * Used to get campaign id after campaign added
	 * 		for further campaign target service request
	 */
	public String getCampaignId(){
		String CampaignId = null;
		try {
	        
	        NodeList id = _XMLDocument.getElementsByTagName("id");
	        CampaignId =  XMLUtils.getCharacterDataFromElement((Element) id.item(0));
	        
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return CampaignId;
	}//getCampaignId
	
}
